<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600' rel='stylesheet'
        type='text/css'>
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/register.css">

    <div class="testbox">
        <h1>Registration</h1>


        <form method="post">
            <label id="icon" for="name"><i class="icon-user"></i></label>
            <input type="text" name="user" id="name" placeholder="User" required />
            <label id="icon" for="name"><i class="icon-shield"></i></label>
            <input type="password" maxlength="16" name="password" id="password" placeholder="Password" required />
            <label id="icon" for="name2"><i class="icon-shield"></i></label>
            <input type="password2" maxlength="16" name="password2" id="name2" placeholder="Password" required />
        
            <p>By clicking Register, you agree on our <a href="#">terms and condition</a>.</p>
            <a href="#" id="btn" class="button">Register</a>
        </form>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/blueimp-md5/2.19.0/js/md5.js"></script>
    <script>
        var  user = document.getElementById('name')
        var  password2 = document.getElementById('password')
        console.log(user);
        console.log(password);
            btn.onclick = function(){
                console.log(123);
              let  password   = md5(password2.value) 
                fetch('/register',{ 
                    method: 'POST', 
                    headers: {
                    'Content-Type': 'application/json'
                    // 'Content-Type': 'application/x-www-form-urlencoded',
    },
                    body: JSON.stringify( { user:user.value , password})
                })
                .then(response => {
                    console.log(  "response",response);
             
                    return  response.json()
                    //  response.json() 解析得到的数据 返回给下一个 then
                })
                .then(data => { 
                    if(data.code ==20000){
                        alert("注册成功")
                        location.href  = "/login"
                    }else{ 
                        alert(data.msg)
                    }


                });



            }
            //  封装成  fetchpr({  })
/* 
 fetchpr({  url  , type , header, data  })  .then (res =>  res)
*/

    </script>
